﻿
Partial Class WebForm_StepQuery_SearchCleanPlan
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLOrganization()
        End If

    End Sub

#Region "GridView事件"

#Region "服務計畫"
    Protected Sub ParentGridView_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles ParentGridView.PageIndexChanging
        Me.ParentGridView.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub ParentGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ParentGridView.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim MyGridView As GridView = TryCast(sender, GridView)
            Dim dat As New clsData
            Dim sql As New MyStringBuilder
            Dim dt As Data.DataTable
            Dim ChildGridView As GridView = TryCast(e.Row.FindControl("ChildGridView"), GridView)

            sql.AppendFormat("select a.*,b.Text as ItemTypeText,c.UserName ")
            sql.AppendFormat("from CleanPlan a ")
            sql.AppendFormat("join CodeMapping b on a.ItemType=b.Value and b.TableName='CleanPlan' and b.ColumnName='ItemType' ")
            sql.AppendFormat("left join [User] c on a.PersonInCharge = c.UserID ")
            sql.AppendFormat("where a.PlanHeaderID='{0}' ", MyGridView.DataKeys(e.Row.RowIndex).Item("PlanHeaderID").ToString())
            sql.AppendFormat("order by a.CreateDate Desc ")

            dt = dat.GetData(sql)

            ChildGridView.DataSource = dt
            ChildGridView.DataBind()
        End If
    End Sub
#End Region

#Region "服務項目"
    Protected Sub ChildGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim MyGridView As GridView = TryCast(sender, GridView)
            Dim dat As New clsData
            Dim sql As New MyStringBuilder
            Dim dt As Data.DataTable
            Dim VOGridView As GridView = TryCast(e.Row.FindControl("VOGridView"), GridView)

            sql.AppendFormat("select distinct a.PlanID,c.VOrganizationName,b.VUOrganizationID from CleanPlanMapping a ")
            sql.AppendFormat("join WorkHeader b ")
            sql.AppendFormat("on a.WorkHeaderID=b.WorkHeaderID ")
            sql.AppendFormat("join VOrganizationView c ")
            sql.AppendFormat("on b.VUOrganizationID=c.UOrganizationID ")
            sql.AppendFormat("where a.PlanID='{0}' ", MyGridView.DataKeys(e.Row.RowIndex).Item("PlanID").ToString())
            sql.AppendFormat("order by c.VOrganizationName ")

            dt = dat.GetData(sql)

            VOGridView.DataSource = dt
            VOGridView.DataBind()
        End If
    End Sub
#End Region

#Region "參與志工隊"
    Protected Sub VOGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim MyGridView As GridView = TryCast(sender, GridView)
            Dim dat As New clsData
            Dim sql As New MyStringBuilder
            Dim dt As Data.DataTable
            Dim WHGridView As GridView = TryCast(e.Row.FindControl("WHGridView"), GridView)

            sql.AppendFormat("select b.*,c.DirectUOrganizationID from CleanPlanMapping a ")
            sql.AppendFormat("join CleanPlanWorkHeaderView b ")
            sql.AppendFormat("on a.WorkHeaderID=b.WorkHeaderID ")
            sql.AppendFormat("left join VOrganizationView c ")
            sql.AppendFormat("on b.VUOrganizationID=c.UOrganizationID ")
            sql.AppendFormat("where a.PlanID='{0}' ", MyGridView.DataKeys(e.Row.RowIndex).Item("PlanID").ToString())
            sql.AppendFormat("and b.VUOrganizationID='{0}' ", MyGridView.DataKeys(e.Row.RowIndex).Item("VUOrganizationID").ToString())
            sql.AppendFormat("order by WorkStartDate desc ")

            dt = dat.GetData(sql)

            WHGridView.DataSource = dt
            WHGridView.DataBind()
        End If
    End Sub
#End Region

#Region "服務成果"

    Protected Sub WHGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lbtnWorkHeader As LinkButton = TryCast(e.Row.FindControl("lbtnWorkHeader"), LinkButton)
            ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(lbtnWorkHeader)
        End If
    End Sub

    Protected Sub WHGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
        If e.CommandName = "WorkHeader" Then
            Dim MyGridView As GridView = TryCast(sender, GridView)
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Me.WorkHeaderID = MyGridView.DataKeys(row.RowIndex).Item("WorkHeaderID").ToString()
            Me.PlanID = MyGridView.DataKeys(row.RowIndex).Item("PlanID").ToString()

            Dim dat As New clsData
            Dim sql As New MyStringBuilder
            Dim dt As Data.DataTable

            sql.AppendFormat("select UserID from RoleUserMapping ")
            sql.AppendFormat("where RoleID in ")
            sql.AppendFormat("( ")
            sql.AppendFormat("	select distinct RoleID from RoleOrganizationMappingView ")
            sql.AppendFormat("	where UOrganizationID in ('{0}','{1}') ", MyGridView.DataKeys(row.RowIndex).Item("VUOrganizationID").ToString(), MyGridView.DataKeys(row.RowIndex).Item("DirectUOrganizationID").ToString())
            sql.AppendFormat(") ")
            sql.AppendFormat("and UserID='{0}' ", clsAuth.GetUserID())
            sql.AppendFormat("Union ")
            sql.AppendFormat("select UserID from OtherOrganizationMapping ")
            sql.AppendFormat("where UserID='{0}' ", clsAuth.GetUserID())
            sql.AppendFormat("and UOrganizationID = '{0}' ", MyGridView.DataKeys(row.RowIndex).Item("VUOrganizationID").ToString())

            dt = dat.GetData(sql)


            If dt.Rows.Count > 0 Then
                RaiseEvent Execute(Me, New EventArgs)
            Else
                RaiseEvent View(Me, New EventArgs)
            End If

        End If
    End Sub
#End Region

#End Region

#Region "繫結資料"
    Private Sub BindDDLOrganization()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select o.* ")
        sql.AppendFormat("from OrganizationView o ")
        sql.AppendFormat("where OrganizationName like '%公所%' and Active = 'Y' ")

        Me.ddlOrganization.DataTextField = "OrganizationName"
        Me.ddlOrganization.DataValueField = "UOrganizationID"
        Me.ddlOrganization.DataSource = MyObj.GetDDLDirectOrganizationData()
        Me.ddlOrganization.DataBind()

        If Me.ddlOrganization.Items.Count > 1 Then
            Me.ddlOrganization.Items.Insert(0, MyObj.GetDDLSelectAllItem())
        End If

    End Sub
#End Region

#Region "方法"
    '服務計畫
    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select a.*,a.OrganizationID as UOrganizationID,b.OrganizationName ")
        sql.AppendFormat(",Cast((a.Year-1911) as varchar) + ' 年度' + a.Subject as NewSubject ")
        sql.AppendFormat("from CleanPlanHeader a ")
        sql.AppendFormat("join OrganizationView b on a.OrganizationID=b.UOrganizationID ")
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat(Me.GetWhereSQL())
        sql.AppendFormat("order by OrganizationName ")
        dt = dat.GetData(sql)

        Me.ParentGridView.DataSource = dt
        Me.ParentGridView.DataBind()
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        If Me.ddlOrganization.SelectedValue <> "" Then
            sql.AppendFormat(" and (a.OrganizationID = '{0}') ", Me.ddlOrganization.SelectedValue)
        End If

        If Me.AnnualControl1.Anaual <> "" Then
            sql.AppendFormat(" and a.Year = {0} ", Me.AnnualControl1.Anaual)
        End If

        Return sql.ToString
    End Function
#End Region

#Region "頁面事件"
    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.ParentGridView.EditIndex = -1
        Me.BindGV()
    End Sub
#End Region

#Region "Raise事件"
    Public Event Execute(ByVal source As Object, ByVal e As EventArgs)
    Public Event View(ByVal source As Object, ByVal e As EventArgs)
#End Region

#Region "屬性"
    Public Property PlanID()
        Get
            Return ViewState("PlanID")
        End Get
        Set(ByVal value)
            ViewState("PlanID") = value
        End Set
    End Property

    Public Property WorkHeaderID()
        Get
            Return ViewState("WorkHeaderID")
        End Get
        Set(ByVal value)
            ViewState("WorkHeaderID") = value
        End Set
    End Property
#End Region

End Class
